<?php

function tooltip_table_header($name, $incrementors = TRUE)
{
  $row = "<tr>
          <th>$name:</th>
          <th>Amount:</th>";
  if($incrementors)
  {
    $row .= "<th>N</th>
          <th>F</th>";
  }
          
  $row .= "</tr>";

  return $row;
}

function drawDistributionTable($cardID, $incrementors = TRUE)
{
  $card_obj = new Card($cardID);
  $distro_obj = $card_obj->getCardDistribution(TRUE);

  if(empty($distro_obj))
  {
    return '<center><h4>Not logged in</h4></center>';
  }
  else if(empty($distro_obj->collections) && empty($distro_obj->decks))
  {
    return '<center><h4>No collection or deck found</h4></center>';
  }

  $even = FALSE;

  $distrotable = '<table id="card_distribution" border="0" cellspacing="0">';
  if(!empty($distro_obj->collections))
  {

    $distrotable .= tooltip_table_header("Collection", $incrementors);

    foreach($distro_obj->collections as $collection)
    {
      $amount = $collection->getAmount($distro_obj->cardid);
      $tr_class = ($even = !$even) ? "tooltip_even" : "tooltip_uneven";
      $distrotable .= '
      <tr class="'.$tr_class.'">
        <td class="deck_name" nowrap="NOWRAP"><a href="collections.php?collectionId='.$collection->getID().'">'.$collection->getName().'<span class="distro_collection_name">'.$collection->getName().'</span></a></td>
        <td id="amount_collection_'.$collection->getID().'_'.$distro_obj->cardid.'" class="collection_amount">'.$amount['normal'].' | '.$amount['foil'].'</td>';
        if($incrementors)
        {
          $distrotable .='
          <td class="amount_changer">
            <img src="./images/up.png" alt="+" onclick="increase_amount_in_collection('.$collection->getID().', '.$distro_obj->cardid.', 0)">
            <img src="./images/down.png" alt="-" onclick="decrease_amount_in_collection('.$collection->getID().', '.$distro_obj->cardid.', 0)">
          </td>
          <td class="foil_amount_changer">
            <img src="./images/up_foil.png" alt="+" onclick="increase_amount_in_collection('.$collection->getID().', '.$distro_obj->cardid.', 1)">
            <img src="./images/down_foil.png" alt="-" onclick="decrease_amount_in_collection('.$collection->getID().', '.$distro_obj->cardid.', 1)">
          </td>';
        }
      $distrotable .='
      </tr>';
    }
  }

  if(!empty($distro_obj->decks))
  {
    $distrotable .= tooltip_table_header("Deck", $incrementors);

    foreach($distro_obj->decks as $deck)
    {
      $amount = $deck->getAmount($distro_obj->cardid);

      $tr_class = ($even = !$even) ? "tooltip_even" : "tooltip_uneven";
      $distrotable .= '
      <tr class="'.$tr_class.'">
        <td class="deck_name" nowrap="NOWRAP"><a href="decks.php?deckid='.$deck->getID().'">'.$deck->getName().'<span class="distro_collection_name">'.$deck->getName().'</span></a></td>
        <td id="amount_collection_'.$deck->getID().'_'.$distro_obj->cardid.'" class="deck_amount">'.$amount['normal'].' | '.$amount['foil'].'</td>';
        if($incrementors)
        {
          $distrotable .='
          <td class="amount_changer">
            <img src="./images/up.png" alt="+" width="10" height="10" onclick="increase_amount_in_collection('.$deck->getID().', '.$distro_obj->cardid.', 0)">
            <img src="./images/down.png" alt="-" width="10" height="10" onclick="decrease_amount_in_collection('.$deck->getID().', '.$distro_obj->cardid.', 0)">
          </td>
          <td class="foil_amount_changer">
            <img src="./images/up_foil.png" alt="+" width="10" height="10" onclick="increase_amount_in_collection('.$deck->getID().', '.$distro_obj->cardid.', 1)">
            <img src="./images/down_foil.png" alt="-" width="10" height="10" onclick="decrease_amount_in_collection('.$deck->getID().', '.$distro_obj->cardid.', 1)">
          </td>';
        }
      $distrotable .='
      </tr>';
    }
  }
  $distrotable .= "</table>";

  return $distrotable;
}

?>
